﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class CustomerService : ICustomerService
    {
        private  IRepository<Customer> _customerRepository;

        public CustomerService(IRepository<Customer> customerRepository)
        {
            Check.Require(customerRepository != null);

            this._customerRepository = customerRepository;
        }

        /// <summary>
        /// Helps view all customers on list view
        /// </summary>
        /// <returns>List of all customers</returns>
        public IList<Customer> GetAllCustomers()
        {
            var customers = _customerRepository.List();

            if (customers == null)
                customers = new List<Customer>();

            Check.Ensure(customers != null);

            return customers.ToList();
        }

        /// <summary>
        /// Helps view customers for search view
        /// </summary>
        /// <param name="where">Lambda condition to get customers</param>
        /// <returns></returns>
        public IList<Customer> GetCustomers(Expression<Func<Customer, bool>> @where)
        {
            var customers = _customerRepository.List(@where);

            if (customers == null)
                customers = new List<Customer>();

            Check.Ensure(customers != null);

            return customers.ToList();
        }

        /// <summary>
        /// Helps view active customers only
        /// </summary>
        /// <returns>All active customers</returns>
        public IList<Customer> GetAllActiveCustomers()
        {
            var customers = _customerRepository.List(x => x.IsActive);

            if (customers == null)
                customers = new List<Customer>();

            Check.Ensure(customers != null);

            return customers.ToList();
        }

        /// <summary>
        /// Helps view details of an customer
        /// </summary>
        /// <param name="id">Identity of customer</param>
        /// <returns>Customer with specified identity</returns>
        public Customer GetCustomer(int id)
        {
            Check.Require(id > 0);

            Customer customer = _customerRepository.Get(id);

            return customer;
        }

        /// <summary>
        /// Insert an customer from view
        /// </summary>
        /// <param name="customer">Customer to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertCustomer(Customer customer)
        {
            Check.Require(customer != null);

            _customerRepository = new DbRepository<Customer>();
            _customerRepository.Add(customer);
            return _customerRepository.SaveChanges();
        }

        /// <summary>
        /// Update an customer from view
        /// </summary>
        /// <param name="customer">Customer to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateCustomer(Customer customer)
        {
            Check.Require(customer != null);

            _customerRepository.Update(customer);
            return _customerRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an customer
        /// </summary>
        /// <param name="customer">Customer to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int InActiveCustomer(Customer customer)
        {
            Check.Require(customer != null);

            customer.IsActive = false;
            _customerRepository.Update(customer);
            return _customerRepository.SaveChanges();
        }
    }
}
